package com.yanhuo.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.yanhuo.common.utils.RedisUtils;
import com.yanhuo.search.common.NoteConstant;
import com.yanhuo.search.service.RecordService;
import com.yanhuo.xo.vo.RecordSearchVo;
import com.yanhuo.xo.vo.RecordSearchVo;
import lombok.extern.slf4j.Slf4j;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author xiaozhao
 */
@Service
@Slf4j
public class esRecordServiceImpl implements RecordService {

//    @Autowired
//    ElasticsearchClient elasticsearchClient;

    @Autowired
    RedisUtils redisUtils;

    @Override
    public List<RecordSearchVo> getRecordByKeyWord(String keyword) {
        try {

            Set<String> set=redisUtils.zRange(NoteConstant.RECOED_INDEX,0,-1);
            List<RecordSearchVo> recordSearchVoList=new ArrayList<>();
            for(String str:set){
                recordSearchVoList.add(JSON.parseObject(str,RecordSearchVo.class));
            }
            final String str=keyword;
            Set<RecordSearchVo> contents=recordSearchVoList.stream().filter(e->e.getContent().contains(str)).collect(Collectors.toSet());
            final String t=keyword.trim();
            contents.addAll(recordSearchVoList.stream().filter(e->e.getHighlightContent().contains(t)).collect(Collectors.toSet()));

            return contents.stream().collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<RecordSearchVo> getHotRecord() {
        List<RecordSearchVo> records = new ArrayList<>();
        try {
            Set<String> list=redisUtils.zReverseRange(NoteConstant.RECOED_INDEX,0,10);
            for(String obj:list){
                records.add(JSON.parseObject(obj,RecordSearchVo.class));
            }
            return records;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void addRecord(String keyword) {
        try {

            Set<String> set=redisUtils.zRange(NoteConstant.RECOED_INDEX,0,-1);
            List<RecordSearchVo> recordSearchVoList=new ArrayList<>();
            for(String str:set){
                recordSearchVoList.add(JSON.parseObject(str,RecordSearchVo.class));
            }
            List<String> contents=recordSearchVoList.stream().map(RecordSearchVo::getContent).collect(Collectors.toList());
            for(RecordSearchVo noteSearch: recordSearchVoList){
                if(noteSearch.getContent().contains(keyword)){
                    redisUtils.zIncrementScore(NoteConstant.RECOED_INDEX,JSON.toJSONString(noteSearch),1);
                }
            }

            if(StringUtils.isNotBlank(keyword)&&!contents.contains(keyword)){
                RecordSearchVo recordSearchVo=new RecordSearchVo();
                recordSearchVo.setSearchCount(1L);
                recordSearchVo.setContent(keyword);
                recordSearchVo.setHighlightContent(keyword.trim());
                redisUtils.zAdd(NoteConstant.RECOED_INDEX,JSON.toJSONString(recordSearchVo),1);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
